//
//  TimeMachine.m
//
//  Created by zengyuan on 23/3/14.
//  Copyright (c) 2014 zengyuan. All rights reserved.
//

#import "TimeMachine.h"

@implementation TimeMachine
@synthesize countdownTime;
+ (TimeMachine *)sharedInstance {
    
    static TimeMachine *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

-(void)startTimer:(int)time{
    
    countdownTime=time;
    
    if (!timer) {
        timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    }
    self.isCounting =YES;
}

-(void)stopTimer{
    if (timer) {
        [timer invalidate];
        timer = nil;
    }
    self.isCounting =NO;

}

-(void)countDown{
    if (countdownTime==0) {
        [self stopTimer];
        return;
    }
    
    countdownTime --;
    [[NSNotificationCenter defaultCenter] postNotificationName:kTimeMachineCountingDownNotification object:nil];
}

@end
